/*
 * 
 * @Author     : Yongsheng.X
 * @Date       : 2019-09-26 17:10
 * @Description: 扫码页面
 *
 */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:platform_service/platform_service.dart';

import 'drop_back.dart';

class ScannerPage extends StatefulWidget {
  const ScannerPage({super.key, this.continueQr = false});
  final bool continueQr;
  @override
  State<ScannerPage> createState() => _ScannerPageState();
}

class _ScannerPageState extends State<ScannerPage> {
  NDScannerController? _controller;
  String _result = '';
  num count = 0;
  bool stop = false;

  @override
  void dispose() {
    // _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Backdrop(
        expanded: _result.isNotEmpty,
        backLayer: NDScannerWidget(
          continueScan: widget.continueQr,
          onScannerCreate: (NDScannerController controller) {
            _controller = controller;
          },
          onResult: (value) {
            print('scan result = $value');
            if (value?.isNotEmpty == true) {
              count++;
              setState(() {
                _result = value! + count.toString();
              });
            }
          },
        ),
        frontLayer: Container(
          color: Colors.blue[50],
          child: Center(
            child: Text(
              _result,
              style: const TextStyle(fontSize: 20, color: Colors.red),
            ),
          ),
        ),
        frontHead: const Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: Text(
                '手动报工',
                style: TextStyle(
                    color: Colors.black,
                    fontWeight: FontWeight.w600,
                    fontSize: 16),
              ),
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (!stop) {
            _controller?.pause();
          } else {
            _controller?.resume();
          }
          stop = !stop;
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}
